﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace QQ.Util
{
    using System.Diagnostics;
    using System.Runtime.InteropServices;

    [Serializable]
    [DebuggerDisplay("Count = {Count}")]
    [ComVisible(false)]

    public class HashMap<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable
    {
        protected SortedList<TKey, TValue> _data = new SortedList<TKey, TValue>();

        protected SortedList _odata = new SortedList();

        public void Add(KeyValuePair<TKey, TValue> item)
        {
            if (_data.ContainsKey(item.Key))
            {
                _data[item.Key] = item.Value;
            }
            else
            {
                _data.Add(item.Key,item.Value);
            }
        }

        public void Clear()
        {
            _data.Clear();      
        }

        public bool Contains(KeyValuePair<TKey, TValue> item)
        {       
           return _data.ContainsKey(item.Key) ? true :false;
        }

        public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
        {
            throw new System.NotImplementedException();         
        }

        public int Count
        {
            get {return  _data.Count; }
        }

        public bool IsReadOnly
        {
            get { throw new System.NotImplementedException(); }
        }

        public bool Remove(KeyValuePair<TKey, TValue> item)
        {
            return _data.Remove(item.Key);
        }

        public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
        {
            return _data.GetEnumerator();
           
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return _data.GetEnumerator();
        }

        public void Add(TKey key, TValue value)
        {
            if (_data.ContainsKey(key))
            {
                _data[key] = value;
            }
            else
            {
                _data.Add(key, value);
            }
        }

        
        public bool ContainsKey(TKey key)
        {
            return _data.ContainsKey(key);
        }

        public ICollection<TKey> Keys
        {
            get { return _data.Keys; }
        }

        public bool Remove(TKey key)
        {
            return _data.Remove(key);
        }

        public bool TryGetValue(TKey key, out TValue value)
        {
            throw new NotImplementedException();
        }

        public ICollection<TValue> Values
        {
            get { return _data.Values; }
        }

        public TValue this[TKey key]
        {
            get
            {  
                return _data.ContainsKey(key) ? _data[key] : default(TValue);          
            }
            set
            {               
                if (_data.ContainsKey(key))
                {
                    _data[key]=value;
                }
                else
                {
                    _data.Add(key, value);
                }
            }
        }

        public void Add(object key, object value)
        {
            if (_odata.ContainsKey(key))
            {
                _odata[key] = value;
            }
            else
            {
                _odata.Add(key, value);
            }           
        }

        public bool Contains(object key)
        {
            return _odata.ContainsKey(key);
        }

        IDictionaryEnumerator IDictionary.GetEnumerator()
        {
            return _odata.GetEnumerator();
        }

        public bool IsFixedSize
        {
            get { return _odata.IsFixedSize; }
        }

        ICollection IDictionary.Keys
        {
            get { return _odata.Keys; }
        }

        public void Remove(object key)
        {
            _odata.Remove(key);
        }

        ICollection IDictionary.Values
        {
            get { return _odata.Values; }
        }

        public object this[object key]
        {
            get
            {
                return _odata.ContainsKey(key) ? _odata[key] : default(TValue);
            }
            set
            {
                if (_odata.ContainsKey(key))
                {
                    _odata[key] = value;
                }
                else
                {
                    _odata.Add(key, value);
                }
            }
        }

        public void CopyTo(Array array, int index)
        {
            _odata.CopyTo(array,index);
        }

        public bool IsSynchronized
        {
            get { return _odata.IsSynchronized; }
        }

        public object SyncRoot
        {
            get { return _odata.SyncRoot; }
        }
    }
}
